home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1994 November / Cd Ware (Nro. 2) - Epimundo.iso / DOS / PG / CLIP.ZIP / CLIP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-07  |  4.0 KB  |  207 lines

  1. /*  Source for Clibboard Utility */
  2. /*  1994 David B. Symolon        */
  3.  
  4. #include <dos.h>
  5. #include <FCNTL.H>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <io.h>
  9. #include <alloc.h>
  10. #include <string.h>
  11. #include "clipbrd.h"
  12.  
  13. /* Number of command line parameters.*/
  14. #define  NUM_PARAM    3
  15.  
  16. /* Function prototypes */
  17. void main();
  18. void ClipB(char, char *);
  19. void readClip(char *);
  20. void pasteClip(char* );
  21.  
  22. /* Usage string */
  23. char szUsage[] =
  24.     "USAGE:\nclip <options> filename\nOPTIONS:\n  c Copy to Clipboard \n  p Paste from clipboard\n\n\nCLEAR to clear Clipboard.\n";
  25. char szMe[] =
  26.  "DOS Clipboard Utility (c)1994 David B. Symolon\n21391 Town Lakes Dr. #1-16, Boca Raton, FL,33486\n\n";
  27. /* Code */
  28. void main(int *argc, char * argv[])
  29. {
  30.     char *ans;
  31.  
  32.  
  33.     if(strcmp(argv[1],"CLOSE") == 0)  /*"Hidden" Call for debugging*/
  34.     {
  35.         printf(szMe);
  36.         CloseClip();
  37.         return;
  38.     }
  39.     if(strcmp(argv[1],"CLEAR") == 0)
  40.     {
  41.         printf(szMe);
  42.         printf("Are you sure y/n?");
  43.         gets(ans);
  44.         if(*ans == 'y' || *ans == 'Y')
  45.         {
  46.          OpenClipBoard();
  47.          ClearClipBoard();
  48.          CloseClip();
  49.         }
  50.         return;
  51.     }
  52.  
  53.     if( *argc < NUM_PARAM ){
  54.         printf(szMe);
  55.         printf(szUsage);
  56.     }
  57.     else if(*argv[1] == 'c' || *argv[1] == 'C')
  58.     {
  59.         printf(szMe);
  60.         ClipB('c',argv[2]);
  61.     }
  62.  
  63.     else if(*argv[1] == 'p' || *argv[1] == 'P')
  64.     {
  65.         printf(szMe);
  66.         ClipB('p',argv[2]);
  67.  
  68.     }
  69. }
  70. /******************************************************************
  71. /*
  72. *    FUNCTION: ClipB(char , char*)
  73. *
  74. *    This function just switches between each clipboard function...
  75. *
  76. */
  77. void ClipB(char type, char *pFIle)
  78. {
  79.   unsigned int iErrorcode;
  80.  
  81.  
  82.     if( (iErrorcode = OpenClipBoard()) == 0 )
  83.     {
  84.         printf("Unable to open clipboard ERROR CODE: %i\n",iErrorcode);
  85.         return;
  86.     }
  87.  
  88.     switch (type)
  89.     {
  90.         case 'c':
  91.             readClip(pFIle);
  92.             break;
  93.         case 'p':
  94.             pasteClip(pFIle);
  95.             break;
  96.         default:
  97.          exit (1);
  98.     }
  99.  
  100.     if( (iErrorcode = CloseClip()) == 0)
  101.         printf("Unable to close clipboard - ERROR CODE: %d\n",iErrorcode);
  102.  
  103.  
  104. }
  105.  
  106. /***********************************************************************/
  107. /*
  108. *
  109. *    FUNCTION: pasteClip(char * pFIle)
  110. *
  111. *    This function pastes frm the Window's Clipboard and places it into
  112. *    the specified file.
  113. *
  114. */
  115. void pasteClip(char *pFIle)
  116. {
  117.   unsigned int iErrorcode;
  118.   void * OutBuffer;
  119.   long      Clipsize;
  120.   long   lClipSize;
  121.   FILE * Out;
  122.  
  123.   if((Clipsize = QueryClip(OEM)) == 0)
  124.   {
  125.     printf("Clipboard is empty!\n");
  126.     return;
  127.   }
  128.  
  129.   else if( ( OutBuffer = (void *)malloc( Clipsize )) == NULL )
  130.   {
  131.     printf("Not enough RAM for buffer!\n");
  132.     return;
  133.   }
  134.  
  135.   if((iErrorcode = PasteFrmClip(OEM, OutBuffer )) == 0)
  136.   {
  137.     printf("Error code from Paste %d\n",iErrorcode);
  138.     return;
  139.   }
  140.  
  141.   if(( Out = fopen(pFIle,"a+b" )) != NULL )
  142.   {
  143.     fwrite( OutBuffer,Clipsize,1,Out );
  144.     fclose(Out);
  145.  
  146.   }
  147.  
  148. }
  149.  
  150. /********************************************************************/
  151. /*
  152. *
  153. *    FUNCTION:  readClip(char * pFIle)
  154. *
  155. *    This function reads a text file and then puts it into the Windows
  156. *    Clipboard to be pasted somwhere ?
  157. *
  158. */
  159. void readClip(char * pFIle)
  160. {
  161.   unsigned int iErrorcode;
  162.   void * InBuffer;
  163.   int    filehandle;
  164.   long      Filesize;
  165.   long   lClipSize;
  166.   FILE * In;
  167.  
  168.   filehandle = open( pFIle, O_RDONLY ); /*Get length of file*/
  169.   Filesize = filelength( filehandle );
  170.   close(filehandle);
  171.  
  172.   if ( (lClipSize = CompactClip( Filesize )) == 0)
  173.   {
  174.     printf("CompactClip returned the following %ld\n",lClipSize);
  175.     return;
  176.   }
  177.   if( lClipSize < Filesize)
  178.   {
  179.     printf("Clipboard not big enough to hold file!");
  180.     return;
  181.   }
  182.   if( (InBuffer = (void *)malloc( Filesize+1 )) == NULL )
  183.   {
  184.     printf("Not enough RAM for buffer!\n");
  185.     return;
  186.   }
  187.  
  188.   if( (iErrorcode = ClearClipBoard()) == 0 )
  189.   {
  190.     printf("Unable to clear clipboard ERROR CODE: %d\n",iErrorcode);
  191.     return;
  192.   }
  193.  
  194.  
  195.   if(( In = fopen(pFIle,"r" )) != NULL )
  196.   {
  197.     fread( InBuffer,Filesize,1,In );
  198.     fclose(In);
  199.  
  200.     if( (iErrorcode = CopyToClip(OEM,Filesize, InBuffer)) == 0)
  201.     {
  202.         printf("This is the code from the copy %i\n",iErrorcode);
  203.         return;
  204.     }
  205.   }/* IF FOPEN*/
  206. }
  207.